package javaConcurrentDemo;

import java.util.concurrent.CountDownLatch;
/**
 * 让一些线程阻塞直到一些线程完成操作后才被唤醒
 * CountDownLatch主要有两个方法，当一个或多个线程调用await方法时，调用线程会被阻塞
 * 其他线程调用countDown方法，会将计数器减1 （调用countDown方法的线程不会被阻塞）
 * 当计数器的值变为0时，因调用await方法，被阻塞的线程会被唤醒，继续执行
 * @author Administrator
 *
 */
public class CountDownLatchDemo {
    public static void sixCountry() throws InterruptedException {
    	 CountDownLatch countDownLatch = new CountDownLatch(6);
         for(int i=1;i<=6;i++) {
      	   new Thread(()->{
      		   System.out.println(Thread.currentThread().getName()+"国被灭");
      		   countDownLatch.countDown();
      	   },CountryEnum.forEach_countryEnum(i).getRetValue()).start();
         }
         
         countDownLatch.await();
         System.out.println("秦国统一六国");
    }
	public static void main(String[] args) throws InterruptedException {
		sixCountry();
      /* CountDownLatch countDownLatch = new CountDownLatch(6);
       for(int i=1;i<=6;i++) {
    	   new Thread(()->{
    		   System.out.println(Thread.currentThread().getName()+"\t 上完自习，离开教室");
    		   countDownLatch.countDown();
    	   },String.valueOf(i)).start();
       }
       
       countDownLatch.await();
       System.out.println("班长最后关门走人");*/
	}

}
